package com.qs.user.ui.activity;

import android.databinding.Observable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.qs.base.adapter.ViewPagerAdaper;
import com.qs.base.base.BaseMyActivity;
import com.qs.base.router.RouterActivityPath;
import com.qs.base.router.RouterFragmentPath;
import com.qs.base.utils.FileUtil;
import com.qs.user.BR;
import com.qs.user.R;
import com.qs.user.databinding.UserMyFileActivityBinding;
import com.qs.user.ui.VM.MyFileItemViewModel;
import com.qs.user.ui.VM.MyFileViewModel;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 我的文件
 *
 * @Author ltzz
 * @Date 2019/8/24
 */
@Route(path = RouterActivityPath.User.MY_FILE_ACTIVITY)
public class MyFileActivity extends BaseMyActivity<UserMyFileActivityBinding, MyFileViewModel> implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private FragmentManager mFragmentManager;

    private ViewPagerAdaper viewPagerAdaper;
    List<Fragment> mFragmentList = new ArrayList<Fragment>();

    @Override
    protected boolean statusBarDarkFont() {
        return true;
    }

    @Override
    protected View titleLayout() {
        return null;
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.user_my_file_activity;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //多选按钮点击
        viewModel.uc.moreSelectUc.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                MyFileFragment myFileFragment = (MyFileFragment) mFragmentList.get(viewModel.showSelect);
                if (viewModel.isSelect) {
                    binding.delete.setVisibility(View.VISIBLE);
                    binding.moreSelect.setText("取消");
                } else {
                    binding.delete.setVisibility(View.GONE);
                    binding.moreSelect.setText("多选");
                }
                myFileFragment.moreSelectClick(viewModel.isSelect);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding.head.setTitleLeftText("我的文件");
        binding.head.setAutoFinish(this);


        mFragmentManager = getSupportFragmentManager();
        viewModel.fileFragment1 = (MyFileFragment) ARouter.getInstance().build(RouterFragmentPath.User.MY_FILE_FRAGMENT).withInt("type", 0).navigation();
        viewModel.fileFragment2 = (MyFileFragment) ARouter.getInstance().build(RouterFragmentPath.User.MY_FILE_FRAGMENT).withInt("type", 1).navigation();
        viewModel.fileFragment3 = (MyFileFragment) ARouter.getInstance().build(RouterFragmentPath.User.MY_FILE_FRAGMENT).withInt("type", 2).navigation();
        mFragmentList.add(viewModel.fileFragment1);
        mFragmentList.add(viewModel.fileFragment2);
        mFragmentList.add(viewModel.fileFragment3);

        viewPagerAdaper = new ViewPagerAdaper(mFragmentManager, mFragmentList);

        binding.allBgO.setOnClickListener(this);
        binding.txtBgO.setOnClickListener(this);
        binding.wordBgO.setOnClickListener(this);

        initViewPager();
    }

    public void initViewPager() {
        binding.viewPg.addOnPageChangeListener(this);
        binding.viewPg.setAdapter(viewPagerAdaper);
        binding.viewPg.setCurrentItem(0);
        binding.viewPg.setOffscreenPageLimit(1);
        setChangText(true, false, false);
    }

    private void setChangText(boolean o, boolean two, boolean thre) {
        if (o)
            viewModel.showSelect = 0;
        if (two)
            viewModel.showSelect = 1;
        if (thre)
            viewModel.showSelect = 2;
        binding.all.setSelected(o);
        binding.allBg.setVisibility(o ? View.VISIBLE : View.INVISIBLE);
        binding.txt.setSelected(two);
        binding.txtBg.setVisibility(two ? View.VISIBLE : View.INVISIBLE);
        binding.word.setSelected(thre);
        binding.wordBg.setVisibility(thre ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.all_bg_o) {
            //全部
            setChangText(true, false, false);
            binding.viewPg.setCurrentItem(0);
        }
        if (id == R.id.txt_bg_o) {
            //txt
            setChangText(false, true, false);
            binding.viewPg.setCurrentItem(1);
        }
        if (id == R.id.word_bg_o) {
            //word
            setChangText(false, false, true);
            binding.viewPg.setCurrentItem(2);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        viewModel.typeF=position;
        switch (position) {
            case 0:
                setChangText(true, false, false);
                break;
            case 1:
                setChangText(false, true, false);
                break;
            case 2:
                setChangText(false, false, true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

}
